const express = require('express')
const app = express()

/**
 * 跨域处理
 */
const cors = require('cors')
app.use(cors())

// 允许来自特定域名的跨域请求
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', 'http://localhost:5173');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  res.setHeader('Access-Control-Allow-Credentials', 'true'); // 允许包含凭证信息的请求
  next();
});

/**
 * 支持post请求，json处理
 */
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

/**
 * 引入目录处理模块 path
 */
const { resolve } = require("path");

/**
 * 静态路径配置
 */
app.use("/source", express.static(resolve(__dirname, "./", "public")))

// 系统业务
const sysUserRouter = require('./router/SysUserRouter')
const sysRoleRouter = require('./router/SysRoleRouter')
const sysMenuRouter = require('./router/SysMenuRouter')
app.use('/api/v1/user', sysUserRouter)
app.use('/api/v1/role', sysRoleRouter)
app.use('/api/v1/menu', sysMenuRouter)

// 内容业务
const contentArticleCategoryRouter = require('./router/ArticleCategoryRouter')
const contentArticleRouter = require('./router/ArticleRouter')
const contentTagsRouter = require('./router/ContentTagsRouter')
app.use('/api/v1/content/category', contentArticleCategoryRouter)
app.use('/api/v1/content/article', contentArticleRouter)
app.use('/api/v1/content/tags', contentTagsRouter)

app.listen(3001, () => {
  console.log('*************************************************')
  console.log('****   服务启动成功: http://localhost: 3001  ****')
  console.log('*************************************************')
})